<?php
require_once ('DataBaseQuery.php');
require_once ('debug.php');
if (!isset($_GET["id_f"]) && !isset($_POST["file"]))
    header('Location: ./welcome.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>AdCo Editor | Collaborative Editor</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="language" content="en" />
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/texthandler.js"></script>
        <script type="text/javascript" src="scripts/sack.js"></script>
        <script type="text/javascript" src="scripts/test.js"></script>
    </head>
    <body>
        <div id="header"><div id="header2">
                <div id="logo">
                    <h1><a href="#">AdCo Editor</a></h1>
                    <p id="subtitle">Online Editor</p>
                </div>
                <div id="menu">
                    <ul>
                        <li><a href="welcome.php">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Gallery</a></li>
                        <li><a href="#">Help</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
            </div></div><!-- header --><!-- header2 -->
        <div id="main"><div id="main2">
                <div id="sidebar">
                    <?php debugArea() ?>
                    <h2>Login</h2>
                    <?php
                    $user = "";
                    if (!isset($_POST["username"]))
                        $user = $_GET["uid"];
                    else
                    {
                        $user = $_POST["username"];
                        if(!file_exists("./files/".$_POST["username"]."/".$_POST["file"]))
                            mkdir("./files/".$_POST["username"]."/".$_POST["file"], 0700);
                        $creatorid = getUserId($_POST["username"]);
                        insertNewFile($_POST["file"], $creatorid);
                    }
                    echo "<b><font color = " . '"green">' . "Welcome " . $user . "</font></b>";
                    echo "<br><input type=button onClick=" . '"parent.location=' . "'welcome.php'" . '"' . " value=" . '"Logout"><br><br>';
                    echo "<div id='rezultat'></div>";
                    ?>
                    <h2>Revision History</h2>
                    <div class="box">
                    <?php
                        if(!isset($_POST["username"]))
                        {
                            $fname = getFileName($_GET["id_f"]);
                            $ownerid = getFileOwner($_GET["id_f"]);
                        }
                        else 
                        {
                            $fname = $_POST["file"];
                            $fileid = getFileId2($_POST["file"]);
                            $ownerid = getFileOwner($fileid);
                            //$create = fopen('./files/'.$_POST["username"].'/'.$fname."/1","a+");
                        }
                        $owner = getUserName($ownerid);
                        if ($handle = opendir('./files/'.$owner.'/'.$fname)) 
                        {
                            echo "Entries:\n";
                                    
                            /* This is the correct way to loop over the directory. */
                            while (false !== ($entry = readdir($handle))) 
                            {
                                if($entry != '.' && $entry != '..' && $entry != '.svn')
                                {
                                    echo "<br><br>".$entry." - ".date ("F d Y H:i:s", filemtime('./files/'.$owner.'/'.$fname));
                                    if(!isset($_POST["username"]))
                                        echo "<input type=button id='eveniment' value='See' onClick='getRezultat(\"phptest.php\","
                                    .'"'.$owner.'",'.'"'.$fname.'",'.$entry.','.$_GET["id_f"].");'><br/>";
                                    else
                                    {
                                        $fileid = getFileId2($_POST["file"]);
                                       echo "<input type=button id='eveniment' value='See' onClick='getRezultat(\"phptest.php\","
                                    .'"'.$owner.'",'.'"'.$fname.'",'.$entry.','.$fileid.");'><br/>"; 
                                    }
                                }
                            }

                            closedir($handle);
                        }
                    ?>
                        
                    </div>
                </div><!-- sidebar -->
                <div id="content">
                    <div class="post">
                        <h2><a href="#">Reply</a></h2>
                        <?php
                        if (isset($_GET["id_f"])) {
                            $user_id = getUserId($_GET["uid"]);
                            $rights = getFileRights($_GET["id_f"], $user_id);
                            $filename = getFileName($_GET["id_f"]);
                            $ownerid = getFileOwner($_GET["id_f"]);
                            $owner = getUserName($ownerid);
                            $filelist = array();
                            if ($handle = opendir('./files/'.$owner.'/'.$filename))         
                                while (false !== ($filelist[] = readdir($handle))){}
                            $latest = max($filelist);
                            $canWrite = "true";
                            if(!strcmp($rights, "read"))
                                    $canWrite = "false";
                            
                            echo '<div id = "info" file="' . $filename . '" owner="' . $owner .
                                    '" file_id="' . $_GET["id_f"] . '" username="' . $_GET["uid"] .'" writer="'. $canWrite .'"/>';
                            
                            echo '<textarea id="textpad" readonly="readonly" spellcheck="false" width="800" cols="50" rows="20"></textarea>'; 
                        } else {
                            $user_id = getUserId($_POST["username"]);
                            $fileid = getFileId2($_POST["file"]);
                            addPermission($fileid, $user_id, 2);
                            $rights = getFileRights($fileid, $user_id);
                            $filename = getFileName($fileid);
                            $ownerid = getFileOwner($fileid);
                            $owner = getUserName($ownerid);
                            
                            $filelist = array();
                            if ($handle = opendir('./files/'.$owner.'/'.$filename))         
                                while (false !== ($filelist[] = readdir($handle))){}
                            $latest = max($filelist);
                            $canWrite = "true";
                            if(!strcmp($rights, "read"))
                                    $canWrite = "false";
                            
                            echo '<div id = "info" file="' . $filename . '" owner="' . $owner .
                                    '" file_id="' . $fileid . '" username="' . $_POST["username"] .'" writer="'. $canWrite .'"/>';
                            
                            echo '<textarea id="textpad" readonly="readonly" spellcheck="false" width="800" cols="50" rows="20"></textarea>'; 
                        }
                        ?> <!-- this hole section has to be rewriten TODO -->
                    </div>
                    <div class="post" id="userList">
                        <table id="usrTable" cellspacing="20">
                            <thead>
                                <tr><th>User</th><th>Line</th></tr>
                            </thead>
                            <tbody id="usrTableBody">
                            </tbody>
                        </table>
                    </div>
                    <div class="post">
                        <h2><a href="#">Available Options</a></h2>
                        <h3>
                            <?php
                                if (isset($_GET["id_f"])){
                                    $rights = getFileRights($_GET["id_f"], $user_id);
                                    $owner = getFileOwner($_GET["id_f"]);
                                    if (!strcmp($rights, "read/write"))
                                        echo '<input type=button onClick=saveFile() value = "Save File"';
                                    if(!strcmp($rights,"read/write/edit_settings"))
                                    {
                                         echo "<input type=button onClick=" . '"parent.location=' . "'settings.php?uid=" . $_GET["uid"] . "&id_f=" . $_GET["id_f"] . "'" . '"' . " value=" . '"Edit Permisions"><br><br>';
                                         echo '<input type=button onClick=saveFile() value = "Save File"';    
                                    }                           
                                }
                                else {
                                    $fileid = getFileId2($_POST["file"]);
                                    $user_id = getUserId($_POST["username"]);
                                    $rights = getFileRights($fileid, $user_id);
                                    $owner = getFileOwner($fileid);
                                    if (!strcmp($rights, "read/write"))
                                        echo '<input type=button onClick=saveFile() value = "Save File"';
                                    if(!strcmp($rights,"read/write/edit_settings"))
                                    {
                                         echo "<input type=button onClick=" . '"parent.location=' . "'settings.php?uid=" . $_POST["username"] . "&id_f=" . $fileid . "'" . '"' . " value=" . '"Edit Permisions"><br><br>';
                                         echo '<input type=button onClick=saveFile() value = "Save File"';    
                                    }
                                }
                            ?>
                        </h3>
                    </div>
                    <div class="post">
                        <h2><a href="#">Revision</a></h2>
                        <form id="form1" name="form1" method="post" action="">
                        <div id="revisionArea"></div>
                        </form>
                    </div>
                </div><!-- content -->
                <div class="clearing">&nbsp;</div>
            </div></div><!-- main --><!-- main2 -->
        <div id="footer"><div id="footer2">
                <p>Copyright &copy; 2011, designed by <a href="http://www.webtemplateocean.com/">WebTemplateOcean.com</a></p>
            </div></div>
        <div style="text-align: center; font-size: 0.75em;">Design downloaded from <a href="http://www.freewebtemplates.com/">free website templates</a>.</div></body>
</html>
